package com.ymatou.iapi.livemanage.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livemanage.service.SaveActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.database.model.YmtLive;
import com.ymttest.database.mongo.LiveBasicInfoBean;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_SaveActivity {
	private static String sellerId = EnvSetup.getData("productsellerid");
	private static String sellerName = EnvSetup.getData("productsellername");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播Admin-编辑保存");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new SaveActivityCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SaveActivity_001() {
		Logger.start(true, "直播Admin-编辑保存");
		try {

			// 1 创建直播
			JSONObject createLiveReq = LiveCallService.createLiveBeginInFuture(sellerName, Integer.parseInt(sellerId));
			int liveId = createLiveReq.getJSONObject("Data").getInt("ActivityId");

			// 直播名称
			String Title = "自动化测试 - 直播名称1";

			// 直播地名称
			String ActivityName = "自动化测试  - 直播地名称1";

			// 直播图片
			String PicUrl = "http://pc1.img.ymatou.com/G01/M00/0A/C9/rBBlD1b0p-2AF1waAAAGdC5-fgw197_c1.jpg";

			// 直播介绍
			String ActivityContent = "自动化测试 - 直播介绍1";

			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("Activity->Title", Title);
			updateMap.put("Activity->ActivityName", ActivityName);
			updateMap.put("Activity->ActivityContent", ActivityContent);
			updateMap.put("Activity->PicUrl", PicUrl);
			updateMap.put("Activity->DelType", 2); // 配送方式(0:暂不选择 1:贝海直邮 2:直邮
													// 3:拼邮)
			updateMap.put("Activity->VideoAction", 1);

			JSONObject saveLiveRet = LiveCallService.saveLive(sellerName, Integer.parseInt(sellerId), liveId,
					updateMap);

			// 返回码检查
			Logger.verifyEquals(200, saveLiveRet.getInt("Code"), "响应报文-返回码");

			// SQL数据库检查
			YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
			YmtLive ymtLive = ymtLiveWapper.selectByLiveId(liveId);

			Logger.verifyEquals(Title, ymtLive.getStitle(), "数据库SQL-sTitle");
			Logger.verifyEquals(ActivityName, ymtLive.getSname(), "数据库SQL-sName");
			Logger.verifyEquals(PicUrl, ymtLive.getSpicurl(), "数据库SQL-sPicur");
			Logger.verifyEquals(ActivityContent, ymtLive.getScontent(), "数据库SQL-sContent");
			Logger.verifyEquals("", ymtLive.getSvideocover(), "数据库SQL-sVideoCove为空");
			Logger.verifyEquals("", ymtLive.getSvideourl(), "数据库SQL-sVideoUrl为空");

			// Mongo数据库检查
			if (YmtMongoDriverFactroy.isOldMongoCheck()) {

				LiveBasicInfoWapper liveBasicInfoWapper = new LiveBasicInfoWapper();
				LiveBasicInfoBean liveBasicInfoBean = liveBasicInfoWapper.getByActivityId(liveId);
				Logger.verifyEquals(Title, liveBasicInfoBean.getTitle(), "oldMongo-Title");
				Logger.verifyEquals(ActivityName, liveBasicInfoBean.getActivityName(), "oldMongo-ActivityName");
				Logger.verifyEquals(PicUrl, liveBasicInfoBean.getActivityPicture(), "oldMongo-ActivityPicture");
				Logger.verifyEquals(ActivityContent, liveBasicInfoBean.getActivityContent(),
						"oldMongo-ActivityContent");
				Logger.verifyIsNull(liveBasicInfoBean.getVideoCover(), "oldMongo-VideoCover为空");
				Logger.verifyIsNull(liveBasicInfoBean.getVideoUrl(), "oldMongo-VideoUrl为空");

			}

			if (YmtMongoDriverFactroy.isNewMongoCheck()) {

				LiveModelWapper liveModelWapper = new LiveModelWapper();
				Map<String, Object> liveBasicInfoBean = liveModelWapper.getActivityId(liveId);
				Logger.verifyEquals(1, liveModelWapper.getCountByActivityId(liveId), "活动数据应该只有1条");

				Logger.verifyEquals(Title, liveBasicInfoBean.get("Title"), "newMongo-Title");
				Logger.verifyEquals(ActivityName, liveBasicInfoBean.get("ActivityName"), "newMongo-ActivityName");
				Logger.verifyEquals(PicUrl, liveBasicInfoBean.get("ActivityPicture"), "newMongo-ActivityPicture");
				Logger.verifyEquals(ActivityContent, liveBasicInfoBean.get("ActivityContent"),
						"newMongo-ActivityContent");
				Logger.verifyIsNull(liveBasicInfoBean.get("VideoCover"), "newMongo-VideoCover为空");
				Logger.verifyIsNull(liveBasicInfoBean.get("VideoUrl"), "newMongo-VideoUrl为空");

			}
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SaveActivity_002() {
		Logger.start(true, "校验直播地无法修改");
		try {

			// 1 创建直播
			JSONObject createLiveReq = LiveCallService.createLiveBeginInFuture(sellerName, Integer.parseInt(sellerId));
			int liveId = createLiveReq.getJSONObject("Data").getInt("ActivityId");

			String sPosition = "闸北1";

			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("Activity->Position", sPosition);

			JSONObject saveLiveRet = LiveCallService.saveLive(sellerName, Integer.parseInt(sellerId), liveId,
					updateMap);

			// 返回码检查
			Logger.verifyEquals(200, saveLiveRet.getInt("Code"), "响应报文-返回码");

			// SQL数据库检查
			YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
			YmtLive ymtLive = ymtLiveWapper.selectByLiveId(liveId);

			Logger.verifyNotEquals(sPosition, ymtLive.getSposition(), "SQL校验直播地未修改");

			// Mongo数据库检查
			if (YmtMongoDriverFactroy.isOldMongoCheck()) {

				LiveBasicInfoWapper liveBasicInfoWapper = new LiveBasicInfoWapper();
				LiveBasicInfoBean liveBasicInfoBean = liveBasicInfoWapper.getByActivityId(liveId);
				Logger.verifyNotEquals(sPosition, liveBasicInfoBean.getShopAddress(), "oldMongo校验直播地未修改");

			}

			if (YmtMongoDriverFactroy.isNewMongoCheck()) {

				LiveModelWapper liveModelWapper = new LiveModelWapper();
				Map<String, Object> liveBasicInfoBean = liveModelWapper.getActivityId(liveId);

				Logger.verifyNotEquals(sPosition, liveBasicInfoBean.get("ShopAddress"), "newMongo校验直播地未修改");

			}
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
